// 计算多少天之后的日期
export function addDays(date: string, days: number, seperator = '-') {
  let oDate = new Date(date).valueOf();
  let afterDate = oDate + days * 24 * 3600 * 1000;
  let nDate = new Date(afterDate);
  let y = nDate.getFullYear().toString().padStart(2, 0);
  let m = (nDate.getMonth() + 1).toString().padStart(2, 0);
  let d = nDate.getDate().toString().padStart(2, 0);
  return `${y}${seperator}${m}${seperator}${d}`
}

export function afterDays(days: number) {
  let oDate = new Date().valueOf();
  let afterDate = oDate + days * 24 * 3600 * 1000;
  let nDate = new Date(afterDate);
  let y = nDate.getFullYear().toString().padStart(2, 0);
  let m = (nDate.getMonth() + 1).toString().padStart(2, 0);
  let d = nDate.getDate().toString().padStart(2, 0);
  return `${y}-${m}-${d}`
}

/**
 * 计算两个日期之间的天数
 * @param dateString1  开始日期 yyyy-MM-dd
 * @param dateString2  结束日期 yyyy-MM-dd
 * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期，返回0
 */
export function getDaysBetween(dateString1: string, dateString2: string) {
  var startDate = Date.parse(dateString1);
  var endDate = Date.parse(dateString2);
  if (startDate > endDate) {
    return 0;
  }
  if (startDate == endDate) {
    return 1;
  }
  var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000) + 1 ;
  return days;
}

// 获取当前日期本周的日期
export function getWeekDates() {
  var new_Date = new Date()
  var timesStamp = new_Date.getTime();
  var currenDay = new_Date.getDay();
  var dates = [];
  for (var i = -1; i < 6; i++) {
    dates.push({
      date: uni.$u.timeFormat(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).getTime()),
      week: getWeek(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).getDay())
    })
  }
  return dates
}

function getWeek(days: number) {
  let week = ''
  switch (days) {
    case 1:
      week = '周一';
      break;
    case 2:
      week = '周二';
      break;
    case 3:
      week = '周三';
      break;
    case 4:
      week = '周四';
      break;
    case 5:
      week = '周五';
      break;
    case 6:
      week = '周六';
      break;
    case 0:
      week = '周日';
      break;
  }
  return week
}


/**
     * 获取两个日期之间的所有日期
     *
     * @param startTime string 起始日期：2021-06-25
     * @param endTime   string 终止日期：2021-06-30
     *
     * @return array [20210625, 20210626, 20210627, 20210628, 20210629, 20210630]
     */
export function getDateBetween(startTime: string, endTime: string) {

  let dateArr = [];
  let i = 0;

  while (startTime <= endTime) {
    dateArr[i] = startTime;

    let timestamp = new Date(startTime).getTime();
    let nDate = timestamp + (24 * 60 * 60 * 1000);

    let Y = new Date(nDate).getFullYear() + '-';
    let m = (new Date(nDate).getMonth() + 1 < 10) ? '0' + (new Date(nDate).getMonth() + 1) + '-' : (new Date(nDate).getMonth() + 1) + '-';
    let d = (new Date(nDate).getDate() < 10) ? '0' + new Date(nDate).getDate() : new Date(nDate).getDate();

    startTime = Y + m + d;
    i++;
  }

  return dateArr;
}